<template>
  <div class="edit-pwd">
    <div class="head">
      <!-- <span class="wk wk-circle-password icon" /> -->
      <span class="text">
        {{ $t('user.components.c550e18caadad5558bf3034297816def') }}
      </span>
    </div>
    <el-form
      ref="form"
      v-loading="loading"
      :model="form"
      :rules="rules"
      label-position="left"
      label-width="120px">
      <el-form-item
        :label="$t('user.components.d0394661707b40101785261a97df3eee')"
        prop="oldPwd">
        <el-input v-model.trim="form.oldPwd" :maxlength="20" type="password" />
      </el-form-item>
      <el-form-item
        :label="$t('user.components.ac9f666de561f55e63dd3a1c8e768056')"
        prop="newPwd">
        <el-input v-model.trim="form.newPwd" :maxlength="20" type="password" />
      </el-form-item>
      <el-form-item
        :label="$t('user.components.c1dbe61d8361fefe527c436030883687')"
        prop="confirmPwd">
        <el-input
          v-model.trim="form.confirmPwd"
          :maxlength="20"
          type="password" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSave">
          {{ $t('user.components.2bd7caa52663813455354048a8ca4d8b') }}
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { adminUsersResetPasswordAPI } from '@/api/user/personCenter'
import { removeAuth } from '@/utils/auth'

export default {
  name: 'EditPwd',
  data() {
    const pwdReg = /^(?=.*[a-zA-Z])(?=.*\d).{6,20}$/
    return {
      loading: false,
      form: {},
      rules: {
        oldPwd: [
          { required: true, message: this.$t('user.components.fcb728330d9899d719ff154f5d8d6801'), trigger: 'blur' },
          { pattern: pwdReg, message: this.$t('user.components.5e35bda3fd9f39dd46384765bcc9e01d'), trigger: 'blur' }
        ],
        newPwd: [
          { required: true, message: this.$t('user.components.0b389440a60c0d4f3280f0b6180edafb'), trigger: 'blur' },
          { pattern: pwdReg, message: this.$t('user.components.1c37567696ced532b72ffca0318f6c2d'), trigger: 'blur' }
        ],
        confirmPwd: [
          { required: true, message: this.$t('user.components.ecb80d38be67dc8e82e14c8552fdd0cf'), trigger: 'blur' },
          { validator: this.validatedConfirmPwd, trigger: 'blur' }
        ]
      }
    };
  },
  computed: {
    ...mapGetters([
      'userInfo'
    ])
  },
  methods: {
    validatedConfirmPwd(rule, value, callback) {
      if (value === '') {
        callback(new Error(this.$t('user.components.2821325d67ce4cd1c60bdc7800f26bd2')))
      } else if (value !== this.form.newPwd) {
        callback(new Error(this.$t('user.components.065b82734dfd411a2c5ba443901d1b04')))
      } else {
        callback()
      }
    },
    handleSave() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          const params = {
            id: this.userInfo.userId,
            oldPwd: this.form.oldPwd,
            newPwd: this.form.newPwd
          }
          adminUsersResetPasswordAPI(params).then(() => {
            this.loading = false
            removeAuth().then(() => {
              this.$confirm(
                this.$t('user.components.e74969e7fea6a094ff76d69079f6a810'),
                this.$t('user.components.98b43569d249fb0a3053303944cf994c'),
                {
                  confirmButtonText: this.$t('user.components.7464d7392efa4d1dbdc7f4be921fe208'),
                  showCancelButton: false,
                  type: 'warning'
                }
              ).then(() => {
                this.$router.push('/login')
              }).catch(() => {
                this.$router.push('/login')
              })
            })
          }).catch(() => {
            this.loading = false
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import "./style";

.edit-pwd {
  width: 100%;
  padding: 22px 25px;
  background-color: white;
}
</style>
